package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.CarouselImages;
import com.itljm.cxrMarket.service.CarouselImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ljm
 * @date 2025/1/2 17:35
 */
@RestController
@RequestMapping("/carouselImages")
public class CarouselImagesController {
    @Autowired
    private CarouselImagesService carouselImagesService;
    // 分页查询轮播图
    @RequestMapping("/info")
    public PageResult<CarouselImages> selectCarouselImagesByPage(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 15;
        }
        Page<CarouselImages> imagesPage = new Page<>(page, limit);
        QueryWrapper<CarouselImages> wrapper = new QueryWrapper<>();

        List<CarouselImages> list = carouselImagesService.selectCarouselImagesByPage(imagesPage, wrapper);
        return new PageResult<>(list, imagesPage.getTotal());
    }

    // 查询轮播图详情
    @RequestMapping("/info/{id}")
    public Result selectCarouselImagesDetail(@PathVariable Integer id) {
        CarouselImages images = carouselImagesService.selectCarouselImagesById(id);
        return Result.ok(images);
    }

    // 添加或修改轮播图
    @RequestMapping("/addOrUpdate")
    public Result addOrUpdateCarouselImages(CarouselImages carouselImages) {
        boolean flag = carouselImagesService.addOrUpdateCarouselImages(carouselImages);
        if (flag) {
            return Result.ok("操作成功");
        } else {
            return Result.fail("操作失败");
        }
    }
}
